﻿using IOA.MES.PdaApi.Models.Request.Auth;
using IOA.MES.PdaApi.Models.Response.Auth;
using System.Linq;
using System.Web.Http;
using IOA.MES.Business;
using IOA.MES.Log.DataAccess.VModel;
using IOA.MES.Log.DataAccess.Enum;
using IOA.MES.DataAccess.VModel;
using IOA.MES.DataAccess.Enum;

namespace IOA.MES.PdaApi.Controllers
{
    /// <summary>
    /// 用户认证
    /// </summary>
    public class AuthController : ApiController
    {
        /// <summary>
        /// 登录
        /// </summary>
        [HttpPost]
        public ResultJson<LoginResponse> Login([FromBody] LoginRequest request)
        {
            var user = new UserMgr().Login(request.UserName, request.Password, request.Language, new VLoginHistory
            {
                ClientType = ClientType.PDA,
                DeviceType = DeviceType.Android,
                ClientIP = BaseEnv.ClientIP,
                UserAgent = System.Web.HttpContext.Current.Request.UserAgent?.ToLower() ?? string.Empty
            }, true);

            return new ResultJson<LoginResponse>(new LoginResponse
            {
                TrueName = user.TrueName,
                UserName = user.UserName,
                Email = user.Email,
                Token = user.Token,
                Roles = user.Roles.Select(_ => new Role
                {
                    RoleId = _.PkId,
                    RoleName = _.RoleName
                }).ToList()
            });
        }

        /// <summary>
        /// 退出登录
        /// </summary>
        [HttpPost]
        public ResultJson<LogoutResponse> Logout([FromBody] LogoutRequest request)
        {
            new UserMgr().Logout(BaseEnv.Token);
            return new ResultJson<LogoutResponse>(null);
        }

        /// <summary>
        /// 获取语言包
        /// </summary>
        [HttpPost]
        public ResultJson<QueryLanguageConfigsResponse> QueryLanguageConfigs([FromBody] QueryLanguageConfigsRequest request)
        {

            return new ResultJson<QueryLanguageConfigsResponse>(null);
        }

        /// <summary>
        /// 查询仓库列表
        /// </summary>
        [HttpPost]
        public ResultJson<QueryWarehousesResponse> QueryWarehouses([FromBody] QueryWarehousesRequest request)
        {
            var warehosues = new WarehouseMgr().QueryUserWarehouses(BaseEnv.User.PkId);
            return new ResultJson<QueryWarehousesResponse>(new QueryWarehousesResponse
            {
                Warehouses = warehosues.Select(_ => new Warehouse
                {
                    WorkshopCode = _.WorkshopCode,
                    WorkshopName = _.WorkshopName,
                    WarehouseCode = _.WarehouseCode,
                    WarehouseName = _.WarehouseName,
                    WarehouseType = _.WarehouseType,
                    IsDefault = _.IsDefault
                }).ToList()
            }, warehosues.Count);
        }

        /// <summary>
        /// 查询车间列表
        /// </summary>
        [HttpPost]
        public ResultJson<QueryWorkshopsResponse> QueryWorkshops([FromBody] QueryWorkshopsRequest request)
        {
            var workshops = new WorkshopMgr().QueryUserWorkshops(BaseEnv.User.PkId);
            return new ResultJson<QueryWorkshopsResponse>(new QueryWorkshopsResponse
            {
                Workshops = workshops.Select(_ => new Workshop
                {
                    WorkshopCode = _.WorkshopCode,
                    WorkshopName = _.WorkshopName
                }).ToList()
            });
        }

        /// <summary>
        /// 查询仓库列表
        /// </summary>
        [HttpPost]
        public ResultJson<SetDefaultWarehouseResponse> SetDefaultWarehouse([FromBody] SetDefaultWarehouseRequest request)
        {
            var user = BaseEnv.User;
            new WarehouseMgr().SetDefaultWarehouse(new VUserWarehouse { WarehouseCode = request.WarehouseCode, UserId = user.PkId, ModifyBy = user.PkId });
            return new ResultJson<SetDefaultWarehouseResponse>();
        }

        /// <summary>
        /// 降级组件
        /// </summary>
        [HttpPost]
        public ResultJson<QueryAllSitesResponse> QueryAllSites([FromBody] QueryAllSitesRequest request)
        {
            var sites = new CommonMgr().QueryEnumTypes(typeof(SiteType));
            return new ResultJson<QueryAllSitesResponse>(new QueryAllSitesResponse { Sites = sites });
        }

        /// <summary>
        /// 终检过站
        /// </summary>
        [HttpPost]
        public ResultJson<object> ViaFinalSite([FromBody] ViaFinalSiteRequest request)
        {
            new ProductViaSiteMgr().ViaFinalSite(request.QLevel, request.BarCodes.Where(_ => !string.IsNullOrWhiteSpace(_)).Select(_ => _.ToUpper().Trim()).Distinct().ToList());
            return new ResultJson<object>();
        }
    }
}
